package com.beizhen.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.beizhen.demo.entity.StuUser;
import com.beizhen.demo.service.StuUserService;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
* 用户表 前端控制器
*/
@Controller
@RequestMapping("/stuUser")
    public class StuUserController {
    @Autowired
    private StuUserService stuUserService;
    @Autowired
    private HttpSession session;

    @RequestMapping("/base_regist")
    public String regist( StuUser stuUser){
        Map<String,String> Map=new HashMap<>();
        String pass=null;
        if (null!=stuUser){
            pass=stuUser.getUPwd();
            String salt= RandomStringUtils.randomNumeric(6,8);
            stuUser.setUSalt(salt);
            stuUser.setUIdentity(8);
            Integer result=stuUserService.base_register(stuUser);
            System.out.println(result);
            if (result>0){
                return "base_skip";
            }
        }
            return "error";

    }

    /*
     *页面跳转---登陆页面
     */
    @RequestMapping("/toLogin")
    public String tologin(){

        return "base_login";
}
    /*
     *页面跳转---注册页面
     */
    @RequestMapping("/toRegister")
    public String toRegister()
    {
        session.removeAttribute("stuUser");
        return "base_register";
    }
    /*
     *页面跳转---个人账户
     */
    @RequestMapping("/toInfo")
    public String toInfo(Map<String,Object> map) {
        return "base_account_Info";
    }


    /**
     * 2020年7月13日22点16分
     * 孟希
     *登陆
     */
    @RequestMapping("/doLogin")
    public String doLogin(@RequestParam(value = "uMailbox",required = false)String uMailbox,
                          @RequestParam(value = "uPhone",required = false )String uPhone,
                          @RequestParam(value = "uPwd",required = false) String uPwd,Map<String,Object> map){
        StuUser stuUser=null;
        if (uMailbox!=null && !uMailbox.equals("")){
            Subject subject = SecurityUtils.getSubject();
            AuthenticationToken token = new UsernamePasswordToken(uMailbox,uPwd);
            subject.login(token);
            return "base_account_Info";
        }
        if (null!=uPhone && !("").equals(uPhone)){
            map.put("uPhone",uPhone);
            stuUser=stuUserService.doLogin(map);
            if (null !=stuUser){
                session.setAttribute("stuUser",stuUser);
                return "base_account_Info";

                /*return "base_reception_program";*/
            }else {
                map.clear();
                map.put("msg","登陆失败，该账号还没注册");
            }
        }

        return "base_login";

    }
    /*
     *个人账户--修改手机号
     * 孟希
     */
    @RequestMapping("/upPhone")
    @ResponseBody
    public String upPhone(@RequestParam("newPhone") String newPhone)
    {
        Map<String,Object> map=new HashMap<>();
        System.out.println(newPhone);
        if (newPhone!=null && !("").equals(newPhone)){

            map.put("uPhone",newPhone);
            StuUser user=(StuUser)session.getAttribute("stuUser");
            map.put("uId",user.getUId());
        }
        if (stuUserService.UpdateUser(map)>0){
            map.clear();
            map.put("result","ok");
        }

        return JSONObject.toJSONString(map);
    }
    /*
    *孟希
     *个人账户--修改手机号
     */
    @RequestMapping("/upEamil")
    @ResponseBody
    public String newEamil(@RequestParam("newEamil") String newEamil)
    {
        Map<String,Object> map=new HashMap<>();
        System.out.println(newEamil);
        if (newEamil!=null && !("").equals(newEamil)){

            map.put("uMailbox",newEamil);
            StuUser user=(StuUser)session.getAttribute("stuUser");
            map.put("uId",user.getUId());
        }
        if (stuUserService.UpdateUser(map)>0){
            map.clear();
            map.put("result","ok");
        }

        return JSONObject.toJSONString(map);
    }
}
